SuperStrict

Rem
bbdoc: Regular expressions
End Rem
Module Otus.RE2

ModuleInfo "Version: 1.01"
ModuleInfo "Author: Google Inc (see re2/CONTRIBUTORS)"
ModuleInfo "Credit: Wrapped by Jan Varho"
ModuleInfo "License: New BSD License (see re2/LICENSE)"

ModuleInfo "History: 1.00 Release"
ModuleInfo "History: Initial release"
ModuleInfo "History: 1.01 Release"
ModuleInfo "History: Fix build on Windows"

Import BRL.Blitz

Import "re2/arena.cpp"
'Import "re2/benchmark.cpp"
Import "re2/bitstate.cpp"
Import "re2/compile.cpp"
Import "re2/dfa.cpp"
Import "re2/filtered_re2.cpp"
Import "re2/hash.cpp"
Import "re2/mimics_pcre.cpp"
Import "re2/nfa.cpp"
Import "re2/onepass.cpp"
Import "re2/parse.cpp"
'Import "re2/pcre.cpp"
Import "re2/perl_groups.cpp"
Import "re2/prefilter.cpp"
Import "re2/prefilter_tree.cpp"
Import "re2/prog.cpp"
'Import "re2/random.cpp"
Import "re2/re2.cpp"
Import "re2/regexp.cpp"
Import "re2/rune.cpp"
Import "re2/simplify.cpp"
'Import "re2/sparse_array_test.cpp"
Import "re2/stringpiece.cpp"
Import "re2/stringprintf.cpp"
Import "re2/strutil.cpp"
'Import "re2/test.cpp"
'Import "re2/thread.cpp"
Import "re2/tostring.cpp"
Import "re2/unicode_casefold.cpp"
Import "re2/unicode_groups.cpp"

Import "wrap.cpp"

Private

Extern

Function re2FullMatch:Int(target:Byte Ptr, pattern:Byte Ptr)
Function re2PartialMatch:Int(target:Byte Ptr, pattern:Byte Ptr)
Function re2Match:Int(target:Byte Ptr, pattern:Byte Ptr, start:Int Var, length:Int Var)
Function re2MatchSub:Int(target:Byte Ptr, pattern:Byte Ptr, start:Int Ptr, length:Int Ptr, num:Int)

Function re2CreateRegEx:Int(pattern:Byte Ptr)
Function re2FreeRegEx(regex:Int)

Function re2FullMatchRe:Int(target:Byte Ptr, regex:Int)
Function re2PartialMatchRe:Int(target:Byte Ptr, regex:Int)
Function re2MatchRe:Int(target:Byte Ptr, regex:Int, start:Int Var, length:Int Var)
Function re2MatchSubRe:Int(target:Byte Ptr, regex:Int, start:Int Ptr, length:Int Ptr, num:Int)

End Extern

Public


Rem
bbdoc: Match full string against a pattern
returns: True if the pattern matches the full string
about:
Use TRegEx objects if you need to use the same pattern often.
End Rem
Function RegExIsFullMatch:Int(target:String, pattern:String)
	Local t:Byte Ptr = target.ToUTF8String()
	Local p:Byte Ptr = pattern.ToUTF8String()
	Local r:Int = re2FullMatch(t, p)
	MemFree p
	MemFree t
	Return r
End Function

Rem
bbdoc: Match string against a pattern
returns: True if the pattern matches any part of the string
about:
Use TRegEx objects if you need to use the same pattern often.
End Rem
Function RegExIsPartialMatch:Int(target:String, pattern:String)
	Local t:Byte Ptr = target.ToUTF8String()
	Local p:Byte Ptr = pattern.ToUTF8String()
	Local r:Int = re2PartialMatch(t, p)
	MemFree p
	MemFree t
	Return r
End Function

Rem
bbdoc: Match string against a pattern
returns: True if the pattern matches any part of the string
about:
Use the start and length variables to find the first match.

Use TRegEx objects if you need to use the same pattern often.
End Rem
Function RegExMatch:Int(target:String, pattern:String, start:Int Var, length:Int Var)
	Local t:Byte Ptr = target.ToUTF8String()
	Local p:Byte Ptr = pattern.ToUTF8String()
	Local r:Int = re2Match(t, p, start, length)
	MemFree p
	MemFree t
	Return r
End Function

Rem
bbdoc: Match string against a pattern, extracting substrings
returns: True if the pattern matches any part of the string
about:
Use the start and length variables to find the submatches.
The first will be the whole first match as with #RegExMatch.
Any substrings that didn't match will have start=-1.

Note: start.length must match length.length!

Use TRegEx objects if you need to use the same pattern often.
End Rem
Function RegExMatchSub:Int(target:String, pattern:String, start:Int[], length:Int[])
	Assert start.length=length.length, "Array sizes do not match!"
	Local t:Byte Ptr = target.ToUTF8String()
	Local p:Byte Ptr = pattern.ToUTF8String()
	Local r:Int = re2MatchSub(t, p, start, length, start.length)
	MemFree p
	MemFree t
	Return r
End Function

Rem
bbdoc: Regular expression
about:
Use #Create to create a TRegEx object from a pattern string and then use it with the various methods.
End Rem
Type TRegEx
	
	Field _regex:Int
	
	Method Delete()
		If _regex Then re2FreeRegEx _regex
	End Method
	
	Rem
	bbdoc: Match against a full string
	returns: True if the regex matches the whole target string
	End Rem
	Method IsFullMatch:Int(target:String)
		Local t:Byte Ptr = target.ToUTF8String()
		Local r:Int = re2FullMatchRe(t, _regex)
		MemFree t
		Return r
	End Method
	
	Rem
	bbdoc: Match against a string
	returns: True if the regex matches any part of the target string
	End Rem
	Method IsPartialMatch:Int(target:String)
		Local t:Byte Ptr = target.ToUTF8String()
		Local r:Int = re2PartialMatchRe(t, _regex)
		MemFree t
		Return r
	End Method
	
	Rem
	bbdoc: Match against a string
	returns: True if the regex matches any part of the target string
	about:
	Use the start and length variables to find the first match.
	End Rem
	Method Match:Int(target:String, start:Int Var, length:Int Var)
		Local t:Byte Ptr = target.ToUTF8String()
		Local r:Int = re2MatchRe(t, _regex, start, length)
		MemFree t
		Return r
	End Method
	
	Rem
	bbdoc: Match against a string, extracting submatches
	returns: True if the regex matches any part of the target string
	about:
	Use the start and length variables to find the submatches.
	The first will be the whole first match as with #Match.
	Any substrings that didn't match will have start=-1.
	
	Note: start.length must match length.length!
	End Rem
	Method MatchSub:Int(target:String, start:Int[], length:Int[])
		Assert start.length=length.length, "Array sizes do not match!"
		Local t:Byte Ptr = target.ToUTF8String()
		Local r:Int = re2MatchSubRe(t, _regex, start, length, start.length)
		MemFree t
		Return r
	End Method
	
	Method MatchC:Int(target:Byte Ptr, start:Int Var, length:Int Var)
		Return re2MatchRe(target, _regex, start, length)
	End Method
	
	Rem
	bbdoc: Create a TRegEx object from a string pattern
	End Rem
	Function Create:TRegEx(pattern:String)
		Local r:TRegEx = New TRegEx
		Local p:Byte Ptr = pattern.ToUTF8String()
		r._regex = re2CreateRegEx(pattern)
		MemFree p
		If Not r._regex Return Null
		Return r
	End Function
	
End Type
